/*   FILE: SearchBox.java
 *   DATE OF CREATION:   Thu Jan 09 15:47:07 2003
 *   AUTHOR :            Emmanuel Pietriga (emmanuel@w3.org)
 *   MODIF:              Emmanuel Pietriga (emmanuel.pietriga@inria.fr)
 *   Copyright (c) Emmanuel Pietriga, 2002. All Rights Reserved
 *   Copyright (c) INRIA, 2004. All Rights Reserved
 *   Licensed under the GNU LGPL. For full terms see the file COPYING.
 *   $Id: SearchBox.java 576 2007-03-29 18:32:53Z epietrig $
 */
/*
* Modified by:          Jin Changjiu
* Modified Date:        Dec.05,2009
*
*/

package zgrviewer;

import gblend.adjlistgraph.FsgIdEntry;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


import javax.swing.JComboBox;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import java.util.Vector;
import gblend.result.ChooseResults;
import org.openide.util.Exceptions;

class SearchBox extends JFrame implements ActionListener{

    static int FRAME_WIDTH = 600;
    static int FRAME_HEIGHT = 200;

    GraphicsManager grMngr;

    GVLoader gvl;
    File dotFile=null;
    private FsgIdEntry cbid =null;
    private ChooseResults cr = new ChooseResults();

    JButton prevBt, nextBt;
    JTextField searchText;
    Vector<FsgIdEntry> idlist = new Vector<FsgIdEntry>();

    JComboBox   jcombox;
    private boolean changed = true;
    
    SearchBox(GVLoader gvloader, ChooseResults crs){
	super();
 
        this.gvl = gvloader;
        this.cr = crs;
    
	Container cp = this.getContentPane();
	cp.setLayout(new GridLayout(2,1));
	JPanel p1 = new JPanel();
	JPanel p2 = new JPanel();
	cp.add(p1);
	cp.add(p2);
   
	p1.add(new JLabel("Result Graph:"));

        idlist = crs.getList();
        jcombox = new javax.swing.JComboBox(idlist);
        jcombox.setSize(100, 60);

        jcombox.addItemListener(
        new ItemListener()
        {
            public void itemStateChanged(ItemEvent e)
            {
                if(changed == true)
                { 
                    cbid = idlist.elementAt(jcombox.getSelectedIndex());
                  
                   try {
                       dotFile = cr.selectedResults(cbid);
                      
                } catch (ClassNotFoundException ex) {
                    Exceptions.printStackTrace(ex);
                } catch (SQLException ex) {
                    Exceptions.printStackTrace(ex);
                } catch (FileNotFoundException ex) {
                    Exceptions.printStackTrace(ex);
                }
                    gvl.reloadFile(dotFile);
                }
            }


            public void actionPerformed(ActionEvent e) {
                throw new UnsupportedOperationException("Not supported yet.");
             
            }
        }
     );
   
        p1.add(jcombox);
   
	prevBt = new JButton("Previous");
	p2.add(prevBt);
	prevBt.addActionListener(this);
	nextBt = new JButton("Next");
	p2.add(nextBt);
	nextBt.addActionListener(this);
	//window
	WindowListener w0=new WindowAdapter(){
		public void windowClosing(WindowEvent e){
		    dispose();
		}
	    };
	this.addWindowListener(w0);
	this.setTitle("Choose Results");
	this.pack();
	this.setResizable(true);
    }

    public void actionPerformed(ActionEvent e){
	 if (e.getSource() == prevBt)
        {
            if(idlist.indexOf(cbid)-1>=0)
              cbid = idlist.elementAt(idlist.indexOf(cbid)-1);
        }
	else
        {
           if(idlist.indexOf(cbid)==-1)
           {
              cbid = idlist.elementAt(0);
           }
           else 
           {
               if(idlist.indexOf(cbid)+1<= idlist.size()-1)//when the first postion is 1, has problem 6.16
                cbid = idlist.elementAt(idlist.indexOf(cbid)+1);
           }

        }

     changed = false;
     jcombox.setSelectedIndex(idlist.indexOf(cbid));

      try {
              dotFile = cr.selectedResults(cbid);
            } catch (ClassNotFoundException ex) {
                Exceptions.printStackTrace(ex);
            } catch (SQLException ex) {
                Exceptions.printStackTrace(ex);
            } catch (FileNotFoundException ex) {
                Exceptions.printStackTrace(ex);
            }
     gvl.reloadFile(dotFile);
     changed = true;//Because the generated graph from itemStateChanged is smaller

   }

}
